/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.pages.role;

import java.util.Arrays;
import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.component.label.FeedbackLabel;
import com.eelpo.cmmp.core.wicket.page.extensions.SimpleWebPage;
import com.eelpo.cmmp.uac.dao.vo.Role;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IRoleManageService;
import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * <pre>
 * 
 * 新增和修改角色页面
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class AddOrEditRole extends SimpleWebPage {

	@SpringBean
	protected IRoleManageService userManageService;

	/**
	 * 
	 * 新增角色构造函数
	 * 
	 */
	public AddOrEditRole() {
		this("[新增]角色", new Role(Status.ENABLED));
	}

	/**
	 * 修改角色构造函数
	 * 
	 * @param user
	 *            角色
	 */
	public AddOrEditRole(final Role user) {
		this("[修改]角色", user);
	}

	/**
	 * @param title
	 *            页面标题
	 * @param user
	 *            新增或修改的角色
	 */
	public AddOrEditRole(String title, final Role user) {
		super(title);

		add(renderFormContainer(user));
	}

	public Form<Role> renderFormContainer(final Role user) {

		Form<Role> form = new Form<Role>("form", new Model<Role>(user));

		renderFeedbackPanel(form);
		renderRoleNameTextField(form);
		renderRoleDescriptionTextArea(form);
		renderRoleStatusDropDownChoice(form);
		renderSaveButton(form);
		renderBackButton(form);
		return form;
	}

	public void renderFeedbackPanel(Form<Role> form) {
		form.add(renderFeedbackPanel("feedback"));
	}

	public void renderRoleNameTextField(Form<Role> form) {
		TextField<String> txtName = new TextField<String>("txtName", new PropertyModel<String>(form.getDefaultModelObject(), "name"));
		txtName.setMarkupId("txtName");
		form.add(new FeedbackLabel("txtNameFeedback", txtName));
		form.add(txtName);
	}

	public void renderRoleDescriptionTextArea(Form<Role> form) {
		form.add(new TextArea<String>("txtDescription", new PropertyModel<String>(form.getDefaultModelObject(), "description")));
	}

	public void renderRoleStatusDropDownChoice(final Form<Role> form) {
		List<Status> dataList = Arrays.asList(new Status[] { Status.ENABLED, Status.DISABLED });
		IModel<Status> model = new PropertyModel<Status>(form.getDefaultModelObject(), "status");
		IChoiceRenderer<Status> choiceRenderer = new ChoiceRenderer<Status>("label", "value");
		DropDownChoice<Status> userStatus = new DropDownChoice<Status>("selStatus", model, dataList, choiceRenderer);
		userStatus.setMarkupId("selStatus");
		form.add(new FeedbackLabel("selStatusFeedback", userStatus));
		form.add(userStatus);
	}

	public void renderSaveButton(final Form<Role> form) {
		Button btnSave = new Button("btnSave") {
			private static final long serialVersionUID = 6855404939131434877L;

			public void onSubmit() {
				try {

					Role user = (Role) form.getDefaultModelObject();

					if (BlankUtils.isBlank(user.getId())) {
						user.setId(userManageService.addRole(user));
					} else {
						userManageService.updateRole(user);
					}

					setResponsePage(new RoleManage());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("保存失败！");
				}
			}
		};

		form.add(btnSave);
	}

	public void renderBackButton(final Form<Role> form) {
		Link<Void> btnBack = new Link<Void>("btnBack") {

			private static final long serialVersionUID = -9129976099407032628L;

			@Override
			public void onClick() {
				setResponsePage(new RoleManage());
			}
		};

		form.add(btnBack);
	}
}
